import requests
from bs4 import BeautifulSoup

# import re


def main():
    '''
    获取视频BV号和标题,以列表的形式返回
    列表内装的是一个个字典,一个字典对应一个视频
    '''
    url = "https://www.bilibili.com"
    r = requests.get(url)
    r.encode = 'utf-8'
    html = r.text
    '''
    调试用代码
    with open('ass.html', mode='w', encoding='utf-8') as file1:
        file1.write(str(html))
    '''
    soup = BeautifulSoup(html, 'html.parser')  # 创建搜索器
    div_infobox_s = soup.findAll('div', attrs={'class': 'info-box'})  # 查找div
    tmpdict = {}
    ls = []
    for i in div_infobox_s:
        tmpdict["video"] = i.a.get("href")[25:]  # 获取a标签,取得href的文本值
        # 得到的文本是"//www.bilibili.com/video/BV1S741117Zg"所以从25位开始取
        # 原代码为 tmpdict["video"] = re.search(r'BV[^"]{8,15}', str(i)).group(0)
        tmpdict["name"] = i.img.get("alt")
        # 原代码为tmpdict["name"] = re.search(r'title="[^"]*"', str(i)).group(0)
        ls.append(dict(tmpdict))
    return ls


if __name__ == "__main__":
    print(main())
